package cn.powerone.scm.controller;

import java.util.ArrayList;
import java.util.List;

import cn.powerone.scm.domain.ScmCustomerOrderItemsWithOrderInfoVO;
import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.service.IScmCustomerOrdersService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmCustomerOrderItems;
import cn.powerone.scm.service.IScmCustomerOrderItemsService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;

/**
 * 客户订单明细Controller
 * 
 * @author PowerOne
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/scm/scmCustomerOrderItems")
public class ScmCustomerOrderItemsController extends BaseController
{
    @Autowired
    private IScmCustomerOrderItemsService scmCustomerOrderItemsService;
    @Autowired
    private IScmCustomerOrdersService scmCustomerOrdersService;

    /**
     * 查询客户订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmCustomerOrderItems scmCustomerOrderItems)
    {
        if (scmCustomerOrderItems.getOrderCode() != null && !scmCustomerOrderItems.getOrderCode().isEmpty()){
            ScmCustomerOrders order = scmCustomerOrdersService.selectScmCustomerOrdersByOrderCode(scmCustomerOrderItems.getOrderCode());
            if (order == null){
                // 返回空列表
                return getDataTable(new ArrayList<>());
            }
            scmCustomerOrderItems.setOrderId(order.getId());
        }
        startPage();
        List<ScmCustomerOrderItems> list = scmCustomerOrderItemsService.selectScmCustomerOrderItemsList(scmCustomerOrderItems);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:list')")
    @GetMapping("/listCustomerOrderItemsWithOrderInfo")
    public TableDataInfo listWithOrderInfoWithOrderInfo(ScmCustomerOrderItemsWithOrderInfoVO scmSupplierOrderItems) {
        startPage();
        List<ScmCustomerOrderItemsWithOrderInfoVO> list = scmCustomerOrderItemsService.selectScmSupplierOrderItemsListWithOrderInfo(scmSupplierOrderItems);
        return getDataTable(list);
    }



    /**
     * 导出客户订单明细列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:export')")
    @Log(title = "客户订单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmCustomerOrderItems scmCustomerOrderItems)
    {
        List<ScmCustomerOrderItems> list = scmCustomerOrderItemsService.selectScmCustomerOrderItemsList(scmCustomerOrderItems);
        ExcelUtil<ScmCustomerOrderItems> util = new ExcelUtil<ScmCustomerOrderItems>(ScmCustomerOrderItems.class);
        util.exportExcel(response, list, "客户订单明细数据");
    }

    /**
     * 获取客户订单明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(scmCustomerOrderItemsService.selectScmCustomerOrderItemsByItemId(itemId));
    }

    /**
     * 新增客户订单明细
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:add')")
    @Log(title = "客户订单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmCustomerOrderItems scmCustomerOrderItems) throws Exception {
        return toAjax(scmCustomerOrderItemsService.insertScmCustomerOrderItems(scmCustomerOrderItems));
    }


    /**
     * 修改客户订单明细
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:edit')")
    @Log(title = "客户订单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmCustomerOrderItems scmCustomerOrderItems) throws Exception {
        return toAjax(scmCustomerOrderItemsService.updateScmCustomerOrderItems(scmCustomerOrderItems));
    }

    /**
     * 删除客户订单明细
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:remove')")
    @Log(title = "客户订单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(scmCustomerOrderItemsService.deleteScmCustomerOrderItemsByItemIds(itemIds));
    }

    /**
     * 拉取客户订单明细
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:edit')")
    @Log(title = "拉取订单明细", businessType = BusinessType.UPDATE)
    @PutMapping("/pull")
    public AjaxResult pull(@RequestBody ScmCustomerOrders scmCustomerOrder)
    {
        return toAjax(scmCustomerOrderItemsService.pullScmCustomerOrderItems(scmCustomerOrder));
    }

    /**
     * 从询价单里选择
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:edit')")
    @Log(title = "选择订单明细", businessType = BusinessType.UPDATE)
    @PutMapping("/addFromQuotes/{quotesIds}")
    public AjaxResult addFromQuotes(@RequestBody ScmCustomerOrders scmCustomerOrder, @PathVariable Long[] quotesIds)
    {
        return toAjax(scmCustomerOrderItemsService.addScmCustomerOrderItemsFromQuotes(scmCustomerOrder, quotesIds));
    }

    /**
     * 从历史订单item里直接添加
     */
    @PreAuthorize("@ss.hasPermi('scm:scmCustomerOrderItems:edit')")
    @Log(title = "选择订单明细", businessType = BusinessType.UPDATE)
    @PutMapping("/addFromOrderItems/{ids}")
    public AjaxResult addFromOrderItems(@RequestBody ScmCustomerOrders scmCustomerOrder, @PathVariable Long[] ids)
    {
        return toAjax(scmCustomerOrderItemsService.addScmCustomerOrderItemsFromOrderItems(scmCustomerOrder, ids));
    }
}
